﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DearLee.Toolkit.Drawing;
using DearLee.Toolkit.Enums;
using DearLee.Toolkit.Globalization;
using DearLee.Toolkit.Web;
using System.IO;
using DearLee.Toolkit.Reflection;

namespace Web {

  public partial class Index : BasePage {

    protected void Page_Load(object sender, EventArgs e) {

      if (!IsPostBack) {

        string visualPath, physicalPath,
               directoryName;

        visualPath = Request.Path;
        physicalPath = Server.MapPath(visualPath);
        directoryName = Path.GetDirectoryName(physicalPath);

        GetDirs(directoryName);
      }
    }

    private void GetDirs(string path) {

      string[] dirs = null;
      
      dirs = Directory.GetDirectories(path);
      
      if (dirs == null || dirs.Length == 0) {
        return;
      }

      foreach (string dirItem in dirs) {

        string lastItem = dirItem.Split('\\').Last<string>();
        Response.Write(lastItem + "/<br />");
        GetFiles(dirItem, lastItem);
      }

      GetFiles(path, string.Empty);
    }

    private void GetFiles(string path, string dir) {

      string[] files = null;

      files = Directory.GetFiles(path);

      if (files == null || files.Length == 0) {
        return;
      }

      foreach (string fileItem in files) {

        if (".aspx" == Path.GetExtension(fileItem)) {
          Response.Write("├── <a href=\"" + (string.IsNullOrEmpty(dir) ? string.Empty : dir + "/") + Path.GetFileName(fileItem) + "\" target=\"_blank\">" + Path.GetFileName(fileItem) + "</a><br />");
        }
      }
    }
  }
}
